package com.ruoyi.web.controller.business.order;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ruoyi.business.base.PageParam;
import com.ruoyi.business.order.domain.JbRefund;
import com.ruoyi.business.order.service.JbRefundService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessErrorEnum;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.web.controller.business.order.factory.JbRefundFactory;
import com.ruoyi.web.controller.business.order.vo.JbRefundVO;
import com.ruoyi.web.core.JoController;
import com.ruoyi.web.core.JoR;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 退款 Controller
 *
 * @author ruoyi
 * @date 2024-10-11
 */
@Api("退款")
@RestController
@RequestMapping("/refund")
public class JbRefundController extends JoController
{
    private static final Logger log = LoggerFactory.getLogger(JbRefundController.class);

    @Autowired
    private JbRefundService service;

    @Autowired
    private JbRefundFactory jbRefundFactory;

    /**
     * 查询退款列表
     */
    @ApiOperation("查询退款列表")
    @PreAuthorize("@ss.hasPermi('refund:list')")
    @GetMapping("/list")
    public TableDataInfo list(PageParam page, JbRefund jbRefund)
    {
        return JoR.rows(jbRefundFactory.list(page, jbRefund));
    }

    /**
     * 导出退款列表
     */
    @ApiOperation("导出退款列表")
    @PreAuthorize("@ss.hasPermi('refund:export')")
    @Log(title = "退款", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PageParam page, JbRefund jbRefund)
    {
        LambdaQueryWrapper<JbRefund> select = new LambdaQueryWrapper<>();
        IPage<JbRefund> iPage = service.page(page.b(), select);

        List<JbRefund> list = iPage.getRecords();
        ExcelUtil<JbRefund> util = new ExcelUtil<JbRefund>(JbRefund.class);
        util.exportExcel(response, list, "退款数据");
    }

    /**
     * 获取退款详细信息
     */
    @ApiOperation("获取退款详细信息")
    @PreAuthorize("@ss.hasPermi('refund:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(jbRefundFactory.getInfo(id));
    }

    /**
     * 获取退款详细信息
     */
    @ApiOperation("获取退款详细信息")
    @PreAuthorize("@ss.hasPermi('refund:query')")
    @GetMapping(value = "/businessCode/{businessCode}")
    public AjaxResult businessCode(@PathVariable("businessCode") String businessCode)
    {
        return success(jbRefundFactory.getInfo(businessCode));
    }

    /**
     * 新增退款
     */
    @ApiOperation("新增退款")
    @PreAuthorize("@ss.hasPermi('refund:add')")
    @Log(title = "退款", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody JbRefundVO jbRefundVO)
    {
        if (jbRefundVO.getCommondityList().isEmpty()) return error("商品不正确");
        if (!jbRefundFactory.add(jbRefundVO)) return AjaxResult.error("数据异常");
        return AjaxResult.success();
    }

    /**
     * 修改退款
     */
    @ApiOperation("修改退款")
    @PreAuthorize("@ss.hasPermi('refund:edit')")
    @Log(title = "退款", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody JbRefundVO jbRefundVO)
    {
        if (jbRefundVO.getCommondityList().isEmpty()) return error("商品不正确");
        if (!jbRefundFactory.edit(jbRefundVO)) return AjaxResult.error("数据异常");
        return AjaxResult.success();
    }

    /**
     * 删除退款
     */
    @ApiOperation("删除退款")
    @PreAuthorize("@ss.hasPermi('refund:remove')")
    @Log(title = "退款", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(jbRefundFactory.remove(ids));
    }

    /**
     * 更新用友编码
     */
    @ApiOperation("更新用友编码")
    @Log(title = "更新用友编码", businessType = BusinessType.DELETE)
    @PutMapping("/tNumberUpdate")
    public AjaxResult tNumberUpdate(@RequestBody JbRefundVO jbRefundVO)
    {
        try
        {
            if (StrUtil.isEmpty(jbRefundVO.getTnumber()))
            {
                return JoR.err(BusinessErrorEnum.MISSING_PARAMETER);
            }
            return JoR.success(jbRefundFactory.tNumberUpdate(jbRefundVO));
        } catch (IOException | InterruptedException e)
        {
            throw new ServiceException(BusinessErrorEnum.YONYOU_DOCUMENT_QUERY_FAILED);
        }
    }
}
